/**
 * 
 */
package com.wordpress.sharebyblog.world.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import com.wordpress.sharebyblog.world.domain.City;

/**
 * @author Bharat Mehta
 *
 */
public class CityDaoImpl extends NamedParameterJdbcDaoSupport implements CityDao {

	private static final String FIND_ALL_CITIES ="select id,name,countrycode,district,population from city";
	private static final String FIND_ALL_CITIES_BY_COUNTRY_CODE = FIND_ALL_CITIES + " where city.countryCode = :countryCode";
	private static final String FIND_CITY_BY_ID = FIND_ALL_CITIES + " where id=:id";
	private static final String FIND_CITY_BY_DISTRICT = FIND_ALL_CITIES + " where countrycode=:countryCode and district=:district";
	
	@Override
	public List<City> findCitiesByCountryCode(String countryCode) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("countryCode", countryCode);
		return getNamedParameterJdbcTemplate().query(FIND_ALL_CITIES_BY_COUNTRY_CODE, map, 
				new BeanPropertyRowMapper<City>(City.class));
	}

	@Override
	public City findCityById(Long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		return getNamedParameterJdbcTemplate().queryForObject(FIND_CITY_BY_ID, map, 
				new BeanPropertyRowMapper<City>(City.class));
	}

	@Override
	public List<City> findCityByDistric(String countryCode, String districtName) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("countryCode", countryCode);
		map.put("district", districtName);
		return getNamedParameterJdbcTemplate().query(FIND_CITY_BY_DISTRICT, map, 
				new BeanPropertyRowMapper<City>(City.class));
	}

	
	
}
